

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>dscribe.utils.batch_create &mdash; DScribe 0.2.2 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/style.css" type="text/css" />
    <link rel="author" title="About these documents" href="../../../about.html" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
              <div class="version">
                0.2.2
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../install.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorials/tutorials.html">Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../doc/modules.html">Documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../citing.html">Citing DScribe</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../about.html">About</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">DScribe</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>dscribe.utils.batch_create</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for dscribe.utils.batch_create</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">multiprocessing</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">scipy.sparse</span> <span class="k">import</span> <span class="n">coo_matrix</span>


<div class="viewcode-block" id="create"><a class="viewcode-back" href="../../../doc/dscribe.utils.html#dscribe.utils.batch_create.create">[docs]</a><span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="n">inp</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;This is the function that is called by each process but with</span>
<span class="sd">    different parts of the data. This function is a module level function</span>
<span class="sd">    (instead of nested within batch_create), because only top level functions</span>
<span class="sd">    are picklable by the multiprocessing library.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">global</span> <span class="n">desc</span>
    <span class="n">samples</span> <span class="o">=</span> <span class="n">inp</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="c1"># descriptor = inp[1]</span>
    <span class="n">pos</span> <span class="o">=</span> <span class="n">inp</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">verbose</span> <span class="o">=</span> <span class="n">inp</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
    <span class="n">proc_id</span> <span class="o">=</span> <span class="n">inp</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>

    <span class="c1"># Create descriptors for the dataset</span>
    <span class="n">n_samples</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">samples</span><span class="p">)</span>
    <span class="n">is_sparse</span> <span class="o">=</span> <span class="n">desc</span><span class="o">.</span><span class="n">_sparse</span>
    <span class="n">n_features</span> <span class="o">=</span> <span class="n">desc</span><span class="o">.</span><span class="n">get_number_of_features</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">is_sparse</span><span class="p">:</span>
        <span class="n">data</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">rows</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">cols</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">results</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="n">n_samples</span><span class="p">,</span> <span class="n">n_features</span><span class="p">))</span>

    <span class="n">old_percent</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">for</span> <span class="n">i_sample</span><span class="p">,</span> <span class="n">sample</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">samples</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">pos</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">vec</span> <span class="o">=</span> <span class="n">desc</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">i_pos</span> <span class="o">=</span> <span class="n">pos</span><span class="p">[</span><span class="n">i_sample</span><span class="p">]</span>
            <span class="n">vec</span> <span class="o">=</span> <span class="n">desc</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">i_pos</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">is_sparse</span><span class="p">:</span>
            <span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">vec</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
            <span class="n">rows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">vec</span><span class="o">.</span><span class="n">row</span> <span class="o">+</span> <span class="n">i_sample</span><span class="p">)</span>
            <span class="n">cols</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">vec</span><span class="o">.</span><span class="n">col</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">results</span><span class="p">[</span><span class="n">i_sample</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">vec</span>

        <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span>
            <span class="n">current_percent</span> <span class="o">=</span> <span class="p">(</span><span class="n">i_sample</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">n_samples</span><span class="o">*</span><span class="mi">100</span>
            <span class="k">if</span> <span class="n">current_percent</span> <span class="o">&gt;=</span> <span class="n">old_percent</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">old_percent</span> <span class="o">=</span> <span class="n">current_percent</span>
                <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Process </span><span class="si">{0}</span><span class="s2">: </span><span class="si">{1:.1f}</span><span class="s2"> %&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">proc_id</span><span class="p">,</span> <span class="n">current_percent</span><span class="p">))</span>

    <span class="k">if</span> <span class="n">is_sparse</span><span class="p">:</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
        <span class="n">rows</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">rows</span><span class="p">)</span>
        <span class="n">cols</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">cols</span><span class="p">)</span>
        <span class="n">results</span> <span class="o">=</span> <span class="n">coo_matrix</span><span class="p">((</span><span class="n">data</span><span class="p">,</span> <span class="p">(</span><span class="n">rows</span><span class="p">,</span> <span class="n">cols</span><span class="p">)),</span> <span class="n">shape</span><span class="o">=</span><span class="p">[</span><span class="n">n_samples</span><span class="p">,</span> <span class="n">n_features</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">results</span></div>


<div class="viewcode-block" id="batch_create"><a class="viewcode-back" href="../../../doc/dscribe.utils.html#dscribe.utils.batch_create.batch_create">[docs]</a><span class="k">def</span> <span class="nf">batch_create</span><span class="p">(</span><span class="n">descriptor</span><span class="p">,</span> <span class="n">samples</span><span class="p">,</span> <span class="n">n_proc</span><span class="p">,</span> <span class="n">positions</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">create_func</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Used to create a descriptor output for multiple samples in parallel and</span>
<span class="sd">    store the result in a n_samples x n_features sparse or dense array.</span>

<span class="sd">    Uses the python multiprocessing library and data parallellism to create the</span>
<span class="sd">    descriptors in parallel.</span>

<span class="sd">    Args:</span>
<span class="sd">        samples:</span>
<span class="sd">        n_proc(int): The number of processes. The data will be split into this many</span>
<span class="sd">            parts and divided into different processes.</span>
<span class="sd">        positions(iterable): Needs to be specified if the given descriptor is</span>
<span class="sd">            local and requires a &#39;positions&#39;-argument in the create-function.</span>
<span class="sd">            Should be a list of positions matching the given &#39;samples&#39;.</span>
<span class="sd">        create_func(function): A custom function for creating the output from</span>
<span class="sd">            each process. If none specified a default function will be used.</span>
<span class="sd">            Takes in one tuple argument &#39;inp&#39; with the following information:</span>
<span class="sd">            * inp[0]: samples</span>
<span class="sd">            * inp[1]: descriptor</span>
<span class="sd">            * inp[2]: verbose parameter</span>
<span class="sd">            * inp[3]: process id number</span>
<span class="sd">            The function should return a 2D array. If descriptor.sparse is set</span>
<span class="sd">            to true, the output should be a scipy.linalg.coo_matrix, otherwise</span>
<span class="sd">            a numpy.ndarray should be returned.</span>
<span class="sd">        verbose (boolean): Whether to report a percentage of the samples</span>
<span class="sd">            covered from each process.</span>

<span class="sd">    Returns:</span>
<span class="sd">        np.ndarray | scipy.sparse.coo_matrix: The descriptor vectors for all</span>
<span class="sd">        samples in a single (n_samples x n_features) array.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Get number of samples and whether the output is sparse or not.</span>
    <span class="n">n_features</span> <span class="o">=</span> <span class="n">descriptor</span><span class="o">.</span><span class="n">get_number_of_features</span><span class="p">()</span>
    <span class="n">is_sparse</span> <span class="o">=</span> <span class="n">descriptor</span><span class="o">.</span><span class="n">_sparse</span>
    <span class="k">global</span> <span class="n">desc</span>
    <span class="n">desc</span> <span class="o">=</span> <span class="n">descriptor</span>

    <span class="c1"># If the descriptor is not flattened, the batch processing cannot be by</span>
    <span class="c1"># this function.</span>
    <span class="n">flatten</span> <span class="o">=</span> <span class="n">descriptor</span><span class="o">.</span><span class="n">_flatten</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">flatten</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s2">&quot;The given descriptor is not specified to have flattened output &quot;</span>
            <span class="s2">&quot;with the &#39;flatten&#39; constructor argument. Cannot save the &quot;</span>
            <span class="s2">&quot;descriptor output in a batch.&quot;</span>
        <span class="p">)</span>

    <span class="c1"># Split the data into roughly equivalent chunks for each process</span>
    <span class="n">k</span><span class="p">,</span> <span class="n">m</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">samples</span><span class="p">),</span> <span class="n">n_proc</span><span class="p">)</span>
    <span class="n">atoms_split</span> <span class="o">=</span> <span class="p">(</span><span class="n">samples</span><span class="p">[</span><span class="n">i</span> <span class="o">*</span> <span class="n">k</span> <span class="o">+</span> <span class="nb">min</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">m</span><span class="p">):(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">k</span> <span class="o">+</span> <span class="nb">min</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">m</span><span class="p">)]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_proc</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">positions</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">positions_split</span> <span class="o">=</span> <span class="p">(</span><span class="n">positions</span><span class="p">[</span><span class="n">i</span> <span class="o">*</span> <span class="n">k</span> <span class="o">+</span> <span class="nb">min</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">m</span><span class="p">):(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">k</span> <span class="o">+</span> <span class="nb">min</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">m</span><span class="p">)]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_proc</span><span class="p">))</span>
        <span class="n">inputs</span> <span class="o">=</span> <span class="p">[(</span><span class="n">x</span><span class="p">,</span> <span class="n">pos</span><span class="p">,</span> <span class="n">verbose</span><span class="p">,</span> <span class="n">proc_id</span><span class="p">)</span> <span class="k">for</span> <span class="n">proc_id</span><span class="p">,</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">pos</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">atoms_split</span><span class="p">,</span> <span class="n">positions_split</span><span class="p">))]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">inputs</span> <span class="o">=</span> <span class="p">[(</span><span class="n">x</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">verbose</span><span class="p">,</span> <span class="n">proc_id</span><span class="p">)</span> <span class="k">for</span> <span class="n">proc_id</span><span class="p">,</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">atoms_split</span><span class="p">)]</span>

    <span class="c1"># Initialize a pool of processes, and tell each process in the pool to</span>
    <span class="c1"># handle a different part of the data</span>
    <span class="n">pool</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">Pool</span><span class="p">(</span><span class="n">processes</span><span class="o">=</span><span class="n">n_proc</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">create_func</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">create_func</span> <span class="o">=</span> <span class="n">create</span>
    <span class="n">vec_lists</span> <span class="o">=</span> <span class="n">pool</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">create_func</span><span class="p">,</span> <span class="n">inputs</span><span class="p">)</span>  <span class="c1"># pool.map keeps the order</span>

    <span class="k">if</span> <span class="n">is_sparse</span><span class="p">:</span>
        <span class="c1"># Put results into one big sparse matrix</span>
        <span class="n">n_samples</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">samples</span><span class="p">)</span>
        <span class="n">row_offset</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">data</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">cols</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">rows</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">i_res</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">vec_lists</span><span class="p">):</span>
            <span class="n">i_res</span> <span class="o">=</span> <span class="n">i_res</span><span class="o">.</span><span class="n">tocoo</span><span class="p">()</span>
            <span class="n">i_n_samples</span> <span class="o">=</span> <span class="n">i_res</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">i_data</span> <span class="o">=</span> <span class="n">i_res</span><span class="o">.</span><span class="n">data</span>
            <span class="n">i_col</span> <span class="o">=</span> <span class="n">i_res</span><span class="o">.</span><span class="n">col</span>
            <span class="n">i_row</span> <span class="o">=</span> <span class="n">i_res</span><span class="o">.</span><span class="n">row</span>

            <span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i_data</span><span class="p">)</span>
            <span class="n">rows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i_row</span> <span class="o">+</span> <span class="n">row_offset</span><span class="p">)</span>
            <span class="n">cols</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i_col</span><span class="p">)</span>

            <span class="c1"># Increase the row offset</span>
            <span class="n">row_offset</span> <span class="o">+=</span> <span class="n">i_n_samples</span>

        <span class="c1"># Saves the descriptors as a sparse matrix</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
        <span class="n">rows</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">rows</span><span class="p">)</span>
        <span class="n">cols</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">cols</span><span class="p">)</span>
        <span class="n">results</span> <span class="o">=</span> <span class="n">coo_matrix</span><span class="p">((</span><span class="n">data</span><span class="p">,</span> <span class="p">(</span><span class="n">rows</span><span class="p">,</span> <span class="n">cols</span><span class="p">)),</span> <span class="n">shape</span><span class="o">=</span><span class="p">[</span><span class="n">n_samples</span><span class="p">,</span> <span class="n">n_features</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
        <span class="n">results</span> <span class="o">=</span> <span class="n">results</span><span class="o">.</span><span class="n">tocsr</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">results</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">vec_lists</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">results</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>